<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->

<!--      <el-form-item label="指标序号" prop="indeId">-->
<!--        <el-input-->
<!--          v-model="queryParams.indeId"-->
<!--          placeholder="请输入指标序号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="组织序号" prop="orgId">-->
<!--        <el-input-->
<!--          v-model="queryParams.orgId"-->
<!--          placeholder="请输入组织序号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="维度值" prop="dataDimension">-->
<!--        <el-input-->
<!--          v-model="queryParams.dataDimension"-->
<!--          placeholder="请输入维度值"-->
<!--          clearable-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="数据值" prop="dataValue">-->
<!--        <el-input-->
<!--          v-model="queryParams.dataValue"-->
<!--          placeholder="请输入数据值"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="维度" prop="dimension">-->
<!--        <el-input-->
<!--          v-model="queryParams.dimension"-->
<!--          placeholder="请输入维度"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />      @keyup.enter.native="handleQuery"-->

<!--      </el-form-item>-->
<!--      <el-form-item label="数据年份" prop="dataYear">-->
<!--        <el-input-->
<!--          v-model="queryParams.dataYear"-->
<!--          placeholder="请输入数据年份"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:data:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="waiting_for_processing"
        >待填写</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-message"
          size="mini"
          @click="under_review"
        >审核中</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="Rejected"
        >已驳回</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据序号" align="center" prop="dataId" v-if="columns[0].visible"/>
      <el-table-column label="指标名称" align="center" prop="ycInde.indeName" v-if="columns[1].visible"/>
      <el-table-column label="部门id" align="center" prop="ycInde.deptId"  v-if="columns[2].visible"/>

      <el-table-column label="组织名称" align="center" prop="ycOrg.orgName" v-if="columns[3].visible"/>
      <el-table-column label="填报部室" align="center" prop="ycInde.deptId" :formatter="formatDeptName" v-if="columns[9].visible"/>
      <el-table-column label="数据值" align="center" prop="dataValue" v-if="columns[4].visible"/>
      <el-table-column label="维度值" align="center" prop="dataDimension" v-if="columns[5].visible"/>
      <el-table-column label="维度" align="center" prop="dimension" v-if="columns[6].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.you_dimension" :value="scope.row.dimension"/>
        </template>
      </el-table-column>

      <el-table-column label="状态值" align="center" prop="status" v-if="columns[7].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_status" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column label="数据年份" align="center" prop="dataYear" v-if="columns[8].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:data:edit']"
          >填写指标值</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:data:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改油厂数据管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" @submit.native.prevent>
        <el-form-item label="指标序号" prop="indeId">
          <el-input v-model="form.indeId" placeholder="请输入指标序号" disabled />
        </el-form-item>
        <el-form-item label="指标名称" prop="indeId">
          <el-select v-model="form.indeId" placeholder="请输入指标名称" disabled >
            <el-option
              v-for="item in ycindeOptions"
              :key="item.indeId"
              :label="item.indeName"
              :value="item.indeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组织名称" prop="orgId">
          <el-select v-model="form.orgId" placeholder="请输入组织名称" disabled >
            <el-option
              v-for="item in ycorgOptions"
              :key="item.orgId"
              :label="item.orgName"
              :value="item.orgId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据年份" prop="dataYear">
          <el-input v-model="form.dataYear" placeholder="请输入数据年份" disabled />
        </el-form-item>
        <el-form-item label="维度值" prop="dataDimension">
          <el-input v-model="form.dataDimension" placeholder="请输入维度值" disabled />
        </el-form-item>
        <el-form-item label="维度" prop="dimension">
          <el-select v-model="form.dimension" placeholder="请选择维度" disabled>
            <el-option
              v-for="dict in dict.type.you_dimension"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据值" prop="dataValue">
          <el-input v-model="form.dataValue" placeholder="请输入数据值" />
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listData, getData, delData, addData, updateData, listDataMore} from "@/api/system/data";
import { getUserProfile } from "@/api/system/user"
import log from "@/views/monitor/job/log";
import {ycindeSelect} from "@/api/system/inde";
import {ycorgSelect} from "@/api/system/org";
import {DeptSelect} from "@/api/system/dept";

export default {
  name: "Data",
  dicts:['you_dimension','data_status','data_status1','up_to_standard','yc_organ_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      ycdeptOptions:[],
      // 油厂数据管理表格数据
      dataList: [],
      // 油厂数据临时表格数据
      tempList: [],
      //指标下拉选项
      ycindeOptions: [],
      //组织下拉选项
      ycorgOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        indeId: null,
        orgId: null,
        dataDimension: null,
        dataValue: null,
        dimension: null,
        status: 0,
        dataYear: null,
        deptId:null
      },
      //列信息
      columns:[
        {key:0,label: `数据序号`,visible: false},
        {key:1,label: `指标名称`,visible: true},
        {key:2,label: `部门id`,visible: false},
        {key:3,label: `组织名称`,visible: true},
        {key:4,label: `数据值`,visible: true},
        {key:5,label: `纬度值`,visible: true},
        {key:6,label: `维度`,visible: true},
        {key:7,label: `状态值`,visible: true},
        {key:8,label: `数据年份`,visible: true},
        {key:9,label: `填报部室`,visible: true}
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getYcIndeSelect();
    this.getYcOrgSelect();
    this.getYcDeptSelect();
  },
  methods: {
    formatDeptName(row){
      for(let i = 0; i < this.ycdeptOptions.length; i++){
        if(this.ycdeptOptions[i].deptId == row.ycInde.deptId){
          return this.ycdeptOptions[i].deptName
        }
      }
    },
    //获取部门下拉选项
    getYcDeptSelect(){
      DeptSelect().then(response =>{
        var deptOptions = [];
        this.ycdeptOptions = response.data;
        deptOptions = this.ycdeptOptions.splice(0, 3)
        // console.log("this.ycdeptOptions",this.ycdeptOptions);
      })
    },
    //获取组织下拉选项
    getYcOrgSelect(){
      ycorgSelect().then(response =>{
        this.ycorgOptions = response.data;
      })
    },
    //获取指标下拉选项
    getYcIndeSelect(){
      ycindeSelect().then(response=>{
        this.ycindeOptions = response.data;
      })
    },
    /** 查询油厂数据管理列表 */
    getList() {
      this.loading = true;
      let deptId;
      getUserProfile().then(response => {
        if (response.roleGroup != "超级管理员") {
          deptId = response.data.deptId;
          console.log(deptId)
          this.queryParams.deptId = deptId;
          listDataMore(this.queryParams).then(response => {
            this.dataList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }else{
          listDataMore(this.queryParams).then(response => {
            this.dataList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dataId: null,
        indeId: null,
        orgId: null,
        dataDimension: null,
        dataValue: null,
        createTime: null,
        dimension: null,
        status: null,
        dataYear: null
      };
      this.resetForm("form");
    },
    /** 待填写按钮操作 */
    waiting_for_processing(){
      this.queryParams.pageNum = 1;
      this.queryParams.status = 0;
      this.getList();
    },

    /** 审核中按钮操作 */
    under_review(){
      this.queryParams.pageNum = 1;
      this.queryParams.status = 1;
      this.getList();
    },

    /** 已驳回按钮操作 */
    Rejected(){
      this.queryParams.pageNum = 1;
      this.queryParams.status = 2;
      this.getList();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.status = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dataId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加油厂数据管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dataId = row.dataId || this.ids
      getData(dataId).then(response => {
        this.form = response.data;
        this.form.dimension = this.form.dimension.toString();
        this.open = true;
        this.title = "填写油厂数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.status = 1;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dataId != null) {
            updateData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dataIds = row.dataId || this.ids;
      this.$modal.confirm('是否确认删除油厂数据管理编号为"' + dataIds + '"的数据项？').then(function() {
        return delData(dataIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/data/export', {
        ...this.queryParams
      }, `data_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
